<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NewsForm
 *
 * @author WIN7
 */
class NewsForm extends CFormModel
{
        public $newstitle;
        public $time;
        public $newscontent;
        public $description;
        public $image;
        /**
	 * Declares the validation rules.
	 * 
	 * 
	 */
	public function rules()
	{
		return array(
		
			array('newstitle, time, newscontent, description', 'required'),
                                                         array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
                                                         array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
        
        public function attributeLabels()
	{		
	}
                    
	/**
	 */
	public function add(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection 
                                 $this->time = strtotime($this->time);
                                 $query="INSERT INTO os_news(news_title,news_time,news_content,news_image,news_description,sale_id) 
                                     VALUE('{$this->newstitle}','{$this->time}','{$this->newscontent}','{$this->image}','{$this->description}',".Yii::app()->session["saleId"].")";
                                 $connection->createCommand($query)->query();   
                                 return true;
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
                        public function edit(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection  
                                 //find store_id from rowId in jqGrid
                                 $query_id="SELECT os_news.news_id, os_news.news_image FROM os_news
                                     WHERE os_news.sale_id =".Yii::app()->session["saleId"];
                                 $command_id = $connection->createCommand($query_id);
                                 $dataReader_id=$command_id->query();   
                                 $rows_news = array();
                                while(($row=$dataReader_id->read())!==false) {
                                    $rows_news[]=$row;
                                }
                                //store_id in os_store table;
                                $rowId = $_GET['id']-1;
                                $news_id = $rows_news[$rowId]['news_id'];
                                //update to database
                                if (!empty($rows_news[$rowId]['news_image']) && empty($this->image)) {
                                $this->image = $rows_news[$rowId]['news_image'];
                                }
                                $this->time = strtotime($this->time);
                                $query="UPDATE os_news as a SET a.news_title='{$this->newstitle}', a.news_time='{$this->time}',a.news_content='{$this->newscontent}', a.news_image='{$this->image}', a.news_description='{$this->description}' 
                                    WHERE a.news_id=".$news_id; 
                                $command=$connection->createCommand($query)->execute();
//                                $command->bindParam(":name",  $this->name,PDO::PARAM_STR);
//                                $command->bindParam(":mobile",  $this->mobile,PDO::PARAM_STR);
//                                $command->bindParam(":address",  $this->address,PDO::PARAM_STR);
//                                $command->bindParam(":lat",  $this->lat,PDO::PARAM_STR);
//                                $command->bindParam(":lng",  $this->lng,PDO::PARAM_STR);
//                                $command->bindParam(":image",  $this->image,PDO::PARAM_STR);
//                                $command->bindParam(":id",$store_id,PDO::PARAM_INT);
//                                $row_count=$command->execute();
//                                $resul=array('data'=>$json,'status'=>'ok');
//                                $string=json_encode($resul);
//                                echo $string;                                                              
                                 return true;
                                
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
                        public function getDatabase() {
                            $connection = Yii::app()->db;
                            $sql = "SELECT * FROM os_news";
                            $data = $connection->createCommand($sql)->query();
                            $rows_news = array();
                            while ($row = $data->read()) {
                                $rows_news[] = $row;
                            }
                            return $rows_news;
                        }
}


?>